package com.zhong.gang.applib.controller;

import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.entity.LocalMedia;
import com.zhong.gang.applib.R;
import com.zhong.gang.applib.ui.adapter.AddImgAdapter;
import com.zhong.gang.applib.ui.preview.PreviewAct;
import com.zhong.gang.applib.utils.DiverItemDecoration;
import com.zhong.gang.applib.utils.PictureUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import static android.app.Activity.RESULT_OK;

public class ImgController {
    public AddImgAdapter initImgAdapter(Activity activity,RecyclerView recyclerView,int type){
        AddImgAdapter addImgAdapter = new AddImgAdapter();
        recyclerView.setLayoutManager(new GridLayoutManager(recyclerView.getContext(),3));
        recyclerView.setAdapter(addImgAdapter);
        addImgAdapter.setOnItemChildClickListener(new ItemClickListener(activity,type));
        DiverItemDecoration.addItemDecoration(recyclerView,recyclerView.getContext(), R.color.transparent,8,DiverItemDecoration.HORIZONTAL);
        return addImgAdapter;
    }

    public void setNeedImg(AddImgAdapter addImgAdapter,Activity activity,boolean needImg,int type,boolean canChecked){
        if (addImgAdapter == null)
            return;
        addImgAdapter.setOnItemChildClickListener(new ItemClickListener(activity,needImg,type));
    }

    private class ItemClickListener implements AddImgAdapter.OnItemChildClickListener {
        private int type;
        private Activity activity;
        private boolean needAddImg = false;
        public ItemClickListener(Activity activity, int type){
            this.activity = activity;
            this.type = type;
        }

        public ItemClickListener(Activity activity,boolean needAddImg, int type){
            this.activity = activity;
            this.type = type;
            this.needAddImg = needAddImg;
        }

        @Override
        public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
            if (needAddImg) {
                PictureUtils.openCamera(activity, type);
            }else if (view.getId() == R.id.image){
                List<String> data = adapter.getData();
                PreviewAct.openAct(activity, (ArrayList<String>) data,position);
            }
        }
    }


    /**
     * 添加图片
     * @param adapter
     * @param str
     */
    public void addImgView(AddImgAdapter adapter, String str, boolean isAction){
        if (adapter.getItemCount() > 0)
            adapter.remove(adapter.getItemCount() - 1);
        if (!TextUtils.isEmpty(str))
            adapter.addData(str);
        if (isAction && adapter.getData().size() <= 0)
            adapter.addData("");
    }


    public void addImgView(AddImgAdapter adapter, String str){
        if (!TextUtils.isEmpty(str))
            adapter.addData(str);
    }

    /**
     * 获取选中的文件
     * @param adapter
     * @return
     */
    public File getFile(AddImgAdapter adapter){
        if (adapter.getItemCount() <= 0)
            return null;
        String str = adapter.getItem(adapter.getItemCount() - 1);
        File file = new File(str);
        if (!file.exists())
            return null;
        return file;
    }

    public File getFile(String str){
        if (TextUtils.isEmpty(str))
            return null;
        File file = new File(str);
        if (file.exists())
            return file;
        return null;
    }




    /**
     *
     */
    public void onActivityResult(AddImgAdapter addImgAdapter, int resultCode, @Nullable Intent data){
        if (resultCode != RESULT_OK || data == null || addImgAdapter == null)
            return;
        List<LocalMedia> localList = PictureSelector.obtainMultipleResult(data);
        if (localList.size() > 0) {
            LocalMedia localMedia = localList.get(0);
            String path = localMedia.getCutPath() != null ? localMedia.getCutPath() : localMedia.getCompressPath() != null
                    ? localMedia.getCompressPath() : localMedia.getPath();
            addImgView(addImgAdapter, path, false);
        }
    }
}
